package com.cyz.admin.controller;

import com.cyz.common.dto.ChangeRoleStatusDto;
import com.cyz.admin.service.RoleService;
import com.cyz.common.dto.RoleDto;
import com.cyz.common.utils.R;
import com.cyz.common.validated.AddGroup;
import com.cyz.common.validated.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "管理端角色相关接口")
@RestController
@RequestMapping("/roles")
public class RolesController {
    @Autowired
    private RoleService roleService;

    @PreAuthorize("@ps.hasPermission('system:role:list')")
    @GetMapping("/queryRoles")
    @ApiOperation(value = "查询角色")
    public R queryRole(){
        return roleService.queryRoles();
    }

    @PreAuthorize("@ps.hasPermission('system:role:list')")
    @GetMapping("/queryRolesAndMenuIds")
    @ApiOperation(value = "查询角色")
    public R queryRolesAndMenuIds(){
        return roleService.queryRolesAndMenuIds();
    }

    @PreAuthorize("@ps.hasPermission('system:role:list')")
    @PutMapping("/updateRoleStatus")
    @ApiOperation(value = "更新角色状态")
    public R updateRoleStatus(@RequestBody ChangeRoleStatusDto crd){
        return roleService.updateRoleStatus(crd);
    }

    @PutMapping("/updateRole")
    @ApiOperation(value = "更新角色")
    @PreAuthorize("@ps.hasPermission('system:role:list')")
    public R updateRole(@Validated(UpdateGroup.class) @RequestBody RoleDto roleDto){
        return roleService.updateRoles(roleDto);
    }

    @PostMapping("/addRole")
    @ApiOperation(value = "增加角色")
    @PreAuthorize("@ps.hasPermission('system:role:list')")
    public R addRole(@Validated(AddGroup.class) @RequestBody RoleDto roleDto){
        return roleService.addRole(roleDto);
    }

    @DeleteMapping("/deleteRoles")
    @ApiOperation(value = "删除角色")
    @PreAuthorize("@ps.hasPermission('system:role:list')")
    public R deleteRoles(Integer[] ids){
        return roleService.deleteRoles(ids);
    }
}
